package com.hliushi.demo.web;

import com.itheima.pinda.auth.server.utils.JwtTokenServerUtils;
import com.itheima.pinda.auth.utils.JwtUserInfo;
import com.itheima.pinda.auth.utils.Token;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author llhuang10
 * @date 2021/8/25 21:25
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private JwtTokenServerUtils jwtTokenServerUtils;


    /**
     * 模拟 用户登录功能，如果登录成功则签发jwt令牌给客户端
     *
     * @return
     */
    @GetMapping("/login")
    public Token login() {
        String userName = "admin";
        String password = "admin123";
        // 查询数据库进行用户名密码校验...

        // 如果校验通过，则为客户端生成jwt令牌
        JwtUserInfo jwtUserInfo = new JwtUserInfo();
        jwtUserInfo.setName(userName);
        jwtUserInfo.setOrgId(10L);
        jwtUserInfo.setUserId(1L);
        jwtUserInfo.setAccount(userName);
        jwtUserInfo.setStationId(20L);
        Token token = jwtTokenServerUtils.generateUserToken(jwtUserInfo, null);

        // 实际应该是在过滤器中进行jwt令牌的解析
        JwtUserInfo userInfo = jwtTokenServerUtils.getUserInfo(token.getToken());
        System.out.println(userInfo);
        // {
        //   "token": "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIiwiYWNjb3VudCI6ImFkbWluIiwibmFtZSI6ImFkbWluIiwib3JnaWQiOjEwLCJzdGF0aW9uaWQiOjIwLCJleHAiOjE2Mjk5MDE2Mzd9.Fl154sYu8SEfMY6lCwoNlbsse9uy340G43qPHcKZKIElJyqqweY_shlziEo06Hx1-feOBwCfg6IFA_6cne_xljf0xG-Z6Bg256IhY1QQBIb743FatJJKsYfm6kpTiX22CgoKcEGIJlUVhh0AXVKJt6iafaN_HjC0sLrhlm3zxYk",
        //   "expire": 3600
        // }
        return token;
    }
}
